<?php


namespace app\api\controller;


use app\api\job\SendNotify;
use app\common\controller\ActionLogs;
use app\common\model\CardConsume;
use app\common\model\CardUserBonus;
use app\common\model\CardUserFee;
use app\common\model\CardUserRecLog;
use app\common\model\Merchant;
use app\common\model\Paylog;
use think\Controller;

class CreateNotifyExtend extends Controller
{


    //会员卡领卡信息异步通知
    public function cardrec_notify($param)
    {


    }

    //积分变动通知

    public function bonus_rec_notify($param,$type='')
    {
        $CardUserBonus = new CardUserBonus();
        $map['bus_orderid'] = $param['bus_orderid'];
        $recinfo = $CardUserBonus->where($map)->find();
        //END
        if ($type=="no_master"){
            $notify_param['notift_type'] = "no_master";
        }else{
            $notify_param['notift_type'] = "normal";
        }
        $notify_param['card_id'] = $recinfo['card_id'];
        $notify_param['code'] = $recinfo['code'];
        $notify_param['openid'] = $recinfo['openid'];
        $notify_param['bus_orderid'] = $param['bus_orderid'];
        $notify_param['action_user'] = $recinfo['action_user'];
        $notify_param['action_time'] = $recinfo['create_time'];
        $notify_param['bonus'] = $recinfo['num'];
        $notify_param['pay_cate'] = $recinfo['pay_cate'];   //支付方式
        $notify_param['cate'] = $recinfo['cate'];           //类别
        $notify_param['source'] = $recinfo['source'];       //渠道
        $notify_param['kuantai'] = $recinfo['kuantai'];     //款台
        $notify_param['remark'] = $recinfo['remark'];       //备注
        $notify_param['return_code'] = "SUCCESS";
        $notify_param['return_msg'] = '';
        $notify_param['result_code'] = 'SUCCESS';
        $notify_param['result_msg'] = '充值成功';

        $notify_param['notify_category'] = "bonus_rec";
        $notify_param['merchantid'] = $param['merchantid'];
        $SendNotify = new SendNotify();
        $SendNotify->task_biz_notify($notify_param);
        return $notify_param;


    }

    public function bonus_con_notify($param,$type='')
    {
        $CardUserBonus = new CardUserBonus();
        $map['bus_orderid'] = $param['bus_orderid'];
        $recinfo = $CardUserBonus->where($map)->find();
        //END
        if ($type=="no_master"){
            $notify_param['notift_type'] = "no_master";
        }else{
            $notify_param['notift_type'] = "normal";
        }
        $notify_param['card_id'] = $recinfo['card_id'];
        $notify_param['code'] = $recinfo['code'];
        $notify_param['openid'] = $recinfo['openid'];           //openid
        $notify_param['bus_orderid'] = $param['bus_orderid'];   //业务单号
        $notify_param['action_user'] = $recinfo['action_user']; //操作人
        $notify_param['action_time'] = $recinfo['create_time']; //时间
        $notify_param['bonus'] = $recinfo['num'];           //数量
        $notify_param['pay_cate'] = $recinfo['pay_cate'];   //支付方式
        $notify_param['cate'] = $recinfo['cate'];           //类别
        $notify_param['source'] = $recinfo['source'];       //渠道
        $notify_param['kuantai'] = $recinfo['kuantai'];     //款台
        $notify_param['remark'] = $recinfo['remark'];       //备注
        $notify_param['return_code'] = "SUCCESS";
        $notify_param['return_msg'] = '';
        $notify_param['result_code'] = 'SUCCESS';
        $notify_param['result_msg'] = '消费成功';

        $notify_param['notify_category'] = "bonus_con";
        $notify_param['merchantid'] = $param['merchantid'];
        $SendNotify = new SendNotify();
        $SendNotify->task_biz_notify($notify_param);
        return $notify_param;


    }

    //余额增加通知
    public function balance_rec_notify($bus_orderid,$type='')
    {
        $CardUserFee = new CardUserFee();
        $CardUserBonus = new CardUserBonus();
        $CardUserRecLog = new CardUserRecLog();
        $map['bus_orderid'] = $bus_orderid;
        $cardreclog = $CardUserRecLog->where($map)->field('give_id,orderid,pay_cate,give_fee_id,rec_fee_id,bonus_id,action_user,create_time,mid')->find();
        $givefee = $CardUserFee->where(['id' => $cardreclog['give_fee_id']])->value('num');
        $recinfo = $CardUserFee->where(['id' => $cardreclog['rec_fee_id']])->find();
        $bonus = $CardUserBonus->where(['id' => $cardreclog['bonus_id']])->value('num');
        if($cardreclog['orderid']){
            $Paylog=new Paylog();
            $orderno=$Paylog->where(['orderid'=>$cardreclog['orderid']])->value('orderno');
            $notify_param['order_no'] =$orderno;
        }else{
            $notify_param['order_no'] ="";
        }
        //END
        if ($type=="no_master"){
            $notify_param['notift_type'] = "no_master";
        }else{
            $notify_param['notift_type'] = "normal";
        }
        $notify_param['notift_order_type'] = "no_orderid";

        $notify_param['balance'] = $givefee + $recinfo['num'];
        $notify_param['give_balance'] = $givefee;
        $notify_param['rec_balance'] = $recinfo['num'];
        $notify_param['give_id'] = $cardreclog['give_id'];
        $notify_param['channel'] = $cardreclog['pay_cate'];
        $notify_param['orderid'] = $cardreclog['orderid'];
        $notify_param['bonus'] = $bonus;
        $notify_param['card_id'] = $recinfo['card_id'];
        $notify_param['code'] = $recinfo['code'];
        $notify_param['openid'] = $recinfo['openid'];
        //END
        $notify_param['pay_cate'] = $recinfo['pay_cate'];   //支付方式
        $notify_param['cate'] = $recinfo['cate'];           //类别
        $notify_param['source'] = $recinfo['source'];       //渠道
        $notify_param['kuantai'] = $recinfo['kuantai'];     //款台
        $notify_param['remark'] = $recinfo['remark'];       //备注
        //END
        $notify_param['bus_orderid'] = $bus_orderid;
        $notify_param['action_user'] = $cardreclog['action_user'];
        $notify_param['action_time'] = $cardreclog['create_time'];
        $notify_param['notify_category'] = "balance_rec";
        $notify_param['merchantid'] = $cardreclog['mid'];
        $notify_param['return_code'] = "SUCCESS";
        $notify_param['return_msg'] = '';
        $notify_param['result_code'] = 'SUCCESS';
        $notify_param['result_msg'] = '充值成功';
        $SendNotify = new SendNotify();
        $SendNotify->task_biz_notify($notify_param);

        return $notify_param;
    }

    public function balance_con_notify($notify_param,$type='')
    {
        $CardUserFee = new CardUserFee();
        $recinfo = $CardUserFee->where(['bus_orderid' => $notify_param['bus_orderid']])->find();
        //END
        if ($type=="no_master"){
            $notify_param['notift_type'] = "no_master";
        }else{
            $notify_param['notift_type'] = "normal";
        }

        $notify_param['card_id'] = $recinfo['card_id'];
        $notify_param['code'] = $recinfo['code'];
        $notify_param['openid'] = $recinfo['openid'];
        $notify_param['balance'] = $recinfo['num'];
        $notify_param['notify_category'] = "balance_con";
        $notify_param['merchantid'] = $recinfo['mid'];
        //END
        $notify_param['pay_cate'] = $recinfo['pay_cate'];   //支付方式
        $notify_param['cate'] = $recinfo['cate'];           //类别
        $notify_param['source'] = $recinfo['source'];       //渠道
        $notify_param['kuantai'] = $recinfo['kuantai'];     //款台
        $notify_param['remark'] = $recinfo['remark'];       //备注
        $SendNotify = new SendNotify();
        $SendNotify->task_biz_notify($notify_param);

        return $notify_param;
    }

    //券核销通知
    public function quancon_notify($param)
    {
        $CardConsume = new CardConsume();
        $info = $CardConsume->where(['code' => $param['code']])->find();
        if ($info) {
            $notify_param['card_type'] = $info['card_type'];
            $notify_param['notify_category'] = "quan_con";
            $notify_param['merchantid'] = $param['merchantid'];
            $notify_param['code'] = $param['code'];
            $notify_param['con_user'] = $info['con_username'];
            $notify_param['con_time'] = $info['create_time'];
            $notify_param['source'] = $info['source'];
            $notify_param['card_id'] = $info['card_id'];
            $notify_param['openid'] = $info['openid'];
            $SendNotify = new SendNotify();
            $SendNotify->task_biz_notify($notify_param);
        }

    }

}